﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.Util;

namespace BotonJXC.Forms
{
    public partial class SettleAccountsMainForm : BaseMdiForm
    {
        private SettleAccounts settleAccounts = null;
        private int pageNo = 0;
        int formtype = 0;
        public SettleAccountsMainForm()
        {
            InitializeComponent();
        }

        private void SettleAccountsMainForm_Load(object sender, EventArgs e)
        {
            formtype = Utils.ToInt(this.Tag.ToString(),0);
            if (formtype == 1)
            {
                this.Text = "待结算款单";
            }
            else
            {
                this.Text = "结算款单";
            }
            
        }

         /// <summary>
        /// 初始化窗体数据
        /// </summary>
        public void initFormData()
        {
            
        }

         /// <summary>
        /// 重置窗体数据
        /// </summary>
        public void resetFormData()
        { }

        /// <summary>
        /// 封装订单信息
        /// </summary>
        public void packagingReturnedBlls(string _type)
        {
            settleAccounts.Contract = (ContractManager)Contracttb.Tag;
            settleAccounts.AmountPaid = Utils.ToDouble(AmountPaidtb.Text,0);
            settleAccounts.ContractAmount = Utils.ToDouble(ContractAmounttb.Text, 0);
            settleAccounts.CperiodPayable = Utils.ToDouble(CperiodPayabletb.Text, 0);
            if("add".Equals(_type)){
                settleAccounts.SettleAccSn = new BillSNGenerator().GeneratorBillSn(BillType.SettleAccounts,BillSource.Finance,LoginForm.Store);
                settleAccounts.Creater = LoginForm.Account;
                settleAccounts.CreateTime = DateTime.Now;
                settleAccounts.OrgDepId = LoginForm.DepId;
                settleAccounts.OrgId = LoginForm.StoreId;
                settleAccounts.PrintTime = 0;
            }
            if ("update".Equals(_type)) {
                settleAccounts.Salesman = LoginForm.Account.Username;
            }
            settleAccounts.CurPerDeduct = Utils.ToDouble(CurPerDeducttb.Text, 0);
            settleAccounts.CurPerReal = Utils.ToDouble(CurPerRealtb.Text, 0);
            settleAccounts.ExpiryDate = ExpiryDatedt.Value;
            settleAccounts.InvoiceSn = InvoiceSntb.Text;
            settleAccounts.PayType = (Payment)(PayTypecb.SelectedItem);
            settleAccounts.Remark = Remarktb.Text;
            settleAccounts.StartDate = StartDatedt.Value;

            if ("audit".Equals(_type)) 
            {
                if (formtype == 1) 
                {
                    settleAccounts.Status = (Int32)Status.AuditPass;
                    settleAccounts.StatusOperater = LoginForm.Account;
                    settleAccounts.StatusRemark = StatusRemark.Text;
                    settleAccounts.StatusTime = DateTime.Now;
                }
                else if (formtype == 2)
                {
                    settleAccounts.Status2 = (Int32)Status.AuditPass;
                    settleAccounts.StatusOperater2 = LoginForm.Account;
                    settleAccounts.StatusRemark2 = StatusRemark2.Text;
                    settleAccounts.StatusTime2 = DateTime.Now;
                }
            }

            settleAccounts.Store = (Store)Storecb.SelectedItem;
            settleAccounts.Supplier = (Supplier)Suppliertb.Tag;
            settleAccounts.TaxRate = Utils.ToDouble(TaxRatetb.Text,0);
            settleAccounts.UrgentLevel = UrgentLeveltb.Text;
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        /// <param name="start"></param>
        public void loadData(int start)
        {
            PageModel<SettleAccounts> pageModellist = new SettleAccountsBLL().findSettleAccountsByCondition(null,null,null,start, 1);
            int pageCount = pageModellist.PageCount;//总页数
            pageNo = start;//当前页数
            if (pageNo == 0)
                lastPageButton.Enabled = false;
            else
                lastPageButton.Enabled = true;
            if ((pageNo + 1) == pageCount)
                nextPageButton.Enabled = false;
            else
                nextPageButton.Enabled = true;

            if (pageModellist.Results.Count > 0)
            {
                settleAccounts = pageModellist.Results[0];
                initFormData();
            }
        }
    }
}
